package com.jochen.core.screen

import com.badlogic.gdx.*
import com.badlogic.gdx.ScreenAdapter
import com.badlogic.gdx.graphics.g2d.BitmapFont
import com.badlogic.gdx.scenes.scene2d.ui.Label
import com.badlogic.gdx.scenes.scene2d.ui.Skin
import com.jochen.core.assets.AssetService
import com.jochen.core.assets.AtlasAsset
import com.jochen.core.assets.FontAsset
import com.jochen.core.assets.SkinAsset
import com.jochen.core.assets.SoundAsset
import com.jochen.core.world.GdxGame
import com.badlogic.gdx.graphics.*;

class LoadScreen(
    game: GdxGame,
    screenManager: ScreenManager
) : BaseScreen(game,screenManager) {
   
    
    override fun enter(){
        val assetService = this.game.assetService
        AtlasAsset.values().forEach { atlasAsset ->
            assetService.queue(atlasAsset)
        }
        SkinAsset.values().forEach { skinAsset ->
            assetService.queue(skinAsset)
        }
        SoundAsset.values().forEach { soundAsset ->
            assetService.queue(soundAsset)
        }
        assetService.queue(FontAsset.MAIN_FONT) 
    }
    
    override fun update(delta: Float){
        if(this.game.assetService.update()){
            initLabelstyle()
            createScreen()
            setScreen(Menu::class)
            destroyScreen(LoadScreen::class)
        }
    }
    
    fun createScreen(){
        addScreen(Menu(this.game,this.screenManager))
        addScreen(SettingsScreen(this.game,this.screenManager))
        addScreen(GameScreen(this.game,this.screenManager))
    }
    
    fun initLabelstyle(){
        SkinAsset.values().forEach{ skinAsset ->
            val sk = game.assetService.get(skinAsset)
            sk.add("font",game.assetService.get(FontAsset.MAIN_FONT))
            var defaultLabel = sk.get("default",Label.LabelStyle::class.java)
            if(defaultLabel == null){
                defaultLabel = Label.LabelStyle()
            }
            defaultLabel.font = sk.get("font",BitmapFont::class.java)
            sk.add("default",defaultLabel)
        }
    }
}